package com.example.demo.service.impl;

import cn.hutool.core.map.MapUtil;
import com.example.demo.mapper.InventoryMapper;
import com.example.demo.pojo.InventoryVO;
import com.example.demo.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    InventoryMapper inventoryMapper;

    @Override
    public PageInfo<InventoryVO> selectGoods(Map<String, Object> params) throws ParseException {
        if (params.get("price") != null) {
            String price = (String) params.get("price");
            BigDecimal bigDecimal = new BigDecimal(price);
            params.put("price", bigDecimal);
        }
        //时间格式转换
        if (params.get("createTimeStart") != null || params.get("createTimeEnd") != null) {
            params.put("createTimeStart",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(params.get("createTimeStart").toString()));
            params.put("createTimeEnd", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) params.get("createTimeEnd")));
        }
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()),Integer.parseInt(params.get("pageSize").toString()) );
        List<InventoryVO> goodsList= inventoryMapper.selectGoods(params);
        PageInfo<InventoryVO> pageInfo = new PageInfo<>(goodsList);
        return pageInfo;
    }
}
